﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Forms;
//using System.IO;
using TagLib;
using TagLib.Id3v2;
/*
Copyright 2010 Mirosław M. Mączka

This file is part of Flac2Wma.

    Flac2Wma is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace Flac2Wma
{

#pragma warning disable 618

    public class TagLibConnector
    {
        private TagLib.File tagFile = null;
        public string tagTrack, tagTitle, tagArtist, tagAlbum, tagYear, tagGenre;
        //
        //
        //
        public bool ReadTags(string fileName)
        {
            bool retVal = true;
            //
            tagTrack = ""; tagTitle = ""; tagArtist = ""; tagAlbum = ""; tagYear = ""; tagGenre = "";
            //
            string[] infoFromDir = fileName.Split(new char[] { '\\' });
            if (infoFromDir.Length >= 3)
            {
                tagAlbum = infoFromDir[infoFromDir.Length - 2].TrimEnd(new char[] { '~' });
                tagArtist = infoFromDir[infoFromDir.Length - 3].Trim();
            }
            //
            try
            {
                //
                if (System.IO.File.Exists(fileName))
                {
                    System.IO.FileAttributes fa = System.IO.File.GetAttributes(fileName);
                    fa = fa & ~System.IO.FileAttributes.ReadOnly;
                    System.IO.File.SetAttributes(fileName, fa);
                }
                //
                //
                tagFile = TagLib.File.Create(fileName);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "TagLib.File.Create(" + fileName + ")");
                return false;
            }
            //
            //
            try
            {
                //TagLib.Id3v2.Tag.DefaultVersion = 4;
                //TagLib.Id3v2.Tag.ForceDefaultVersion = true;
                //
                //tagFile.Tag.Track
                //TagLib.Id3v1.Tag t1 = (TagLib.Id3v1.Tag)tagFile.GetTag(TagTypes.Id3v1);
                //TagLib.Id3v2.Tag t2 = (TagLib.Id3v2.Tag)tagFile.GetTag(TagTypes.Id3v2);
                //
                //if(t1.Track
                //
                //TagLib.Id3v2.Tag t1 = tagFile.GetTag(TagTypes.Id3v2);
                //
                tagTitle = tagFile.Tag.Title;
                //
                tagAlbum = tagFile.Tag.Album;
                //
                tagTrack = string.Format("{0:00}", tagFile.Tag.Track);
                //
                if (tagFile.Tag.AlbumArtists.GetLength(0) > 0)
                {
                    tagArtist = string.Join(", ", tagFile.Tag.AlbumArtists);
                }
                else
                {
                    //
                    tagArtist = string.Join(", ", tagFile.Tag.Artists);
                    //
                }
                //
                //
                if (0 != tagFile.Tag.Year) tagYear = tagFile.Tag.Year.ToString();
                //
                tagGenre = string.Join(", ", tagFile.Tag.Genres);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "TagLib Error");
                retVal = false;
            }
            finally
            {
                tagFile = null;
            }
            //
            if (retVal)
            {
                // awaryjne sprawdzenie, czy jest jakaś informacja w starch tagach, której nie ma w nowych
                Mp3OldTag.ReadOldTag(fileName, ref tagTrack, ref tagTitle, ref tagArtist, ref tagAlbum, ref tagYear, ref tagGenre);
            }
            //
            //
            return retVal;
        }
        //
        public bool WriteTags(string fileName)
        {
            bool retVal = true;
            try
            {
                uint u = 0;
                tagFile = TagLib.File.Create(fileName);
                TagLib.Id3v2.Tag.DefaultVersion = 3;
                TagLib.Id3v2.Tag.ForceDefaultVersion = true;
                //tagFile.TagTypes
                if (uint.TryParse(tagTrack, out u)) tagFile.Tag.Track = u;
                tagFile.Tag.Title = tagTitle;

                //if (0 != tagFile.Tag.AlbumArtists.Length)
                {
                    tagFile.Tag.AlbumArtists = new string[] { tagArtist };
                }
                tagFile.Tag.Artists = new string[] { tagArtist };

                tagFile.Tag.Album = tagAlbum;
                if (uint.TryParse(tagYear, out u)) tagFile.Tag.Year = u;
                //
                if (0 == tagFile.Tag.Genres.GetLength(0))
                {
                    tagFile.Tag.Genres = new string[] { tagGenre };
                }
                else
                {
                    //tagFile.Tag.Genres[0] = tagGenre;
                    //tagFile.Tag.Genres = null;
                    tagFile.Tag.Genres = new string[] { tagGenre };
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "TagLib.File.Create(" + fileName + ")");
                tagFile = null;
            }
            //
            if (null == tagFile) return false;
            //
            try
            {
                //tagFile.RemoveTags(TagLib.TagTypes.Id3v1);
                tagFile.Save();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "TagLib.File.Save(" + fileName + ")");
                retVal = false;
            }
            finally
            {
                tagFile = null;
            }
            //
            if (retVal)
            {
                // usunięcie starych tagów z końca pliku
                Mp3OldTag.CutOldTag(fileName);
            }
            //
            return retVal;
        }
        //
        public bool WriteTags(string fileName, string tagTrack, string tagTitle, string tagArtist, string tagAlbum, string tagYear, string tagGenre)
        {
            this.tagTrack = tagTrack;
            this.tagTitle = tagTitle;
            this.tagArtist = tagArtist;
            this.tagAlbum = tagAlbum;
            this.tagYear = tagYear;
            this.tagGenre = tagGenre;
            //
            return WriteTags(fileName);
        }

        //
    }//connector
}//namespace
